--[[--
Here's an example of a more complex Metastrike weapon.
--]]--
SWEP.Base = "msw_base"
SWEP.PrintName = "Ion Charge Blaster" --Again, keep the printname in the sharedspace.
SWEP.Slot = 1 --Like before, use 1 for primary, and 2 for secondary. 
SWEP.ViewModel = "models/weapons/v_pistol.mdl"
SWEP.WorldModel = "models/weapons/w_pistol.mdl"

SWEP.HoldType = "pistol"

SWEP.Primary.Sound          = Sound( "metastrike/weapons/lasergun.wav" )
SWEP.Primary.Recoil         = 1.5
SWEP.Primary.Damage         = 15 -- Base damage
SWEP.Primary.ChargeTime		= 2.5 -- How many seconds it's gonna take us to charge fully.
SWEP.Primary.MaxCharge		= 75 -- How much damage fully-charged should be.
SWEP.Primary.NumShots       = 1
SWEP.Primary.Cone           = 0.035
SWEP.Primary.Delay          = 1.5
SWEP.Primary.ClipSize       = -1
SWEP.Primary.DefaultClip    = 75 --You can have a little leeway with clipless weapons. Try to balance the damage with the ammo.
SWEP.Primary.Automatic      = false
SWEP.Primary.Ammo           = "pistol" 

--[[--
Here's some useful functions you should know when making a complex custom weapon:

GAMEMODE:MSCheckForMod("modname")
Returns true if the Strike Mod is in effect.

SWEP:GetModDamage(OPTIONAL:dmg)
Convenience function. Returns the value dmg (or SWEP.Primary.Damage) as modified by Strike Mods.

SWEP:GetModDelay(OPTIONAL:del)
Convenience function. Returns the value del (or SWEP.Primary.Delay) as modified by Strike Mods.

SWEP:GetModCone(OPTIONAL:con)
Convenience function. Returns the value con (or SWEP.Primary.Cone) as modified by Strike Mods.
--]]--

if CLIENT then

function SWEP:DrawHUD()
	surface.SetDrawColor( 0,0,0,255 )
	surface.DrawRect( ScrW()*0.8,ScrH()*0.8,102,12)
	if self:GetCharge()>0 then
		surface.SetDrawColor( 255,0,0,255 )
		surface.DrawRect( ScrW()*0.8+1,ScrH()*0.8+1,100*(self:GetCharge()/self.Primary.MaxCharge),10)
	end
end

SWEP.ViewModelFlip   = false
SWEP.CSMuzzleFlashes = false

end

function SWEP:SetupDataTables()
	self:NetworkVar("Float",0,"Charge")
end

function SWEP:PrimaryAttack()

end

function SWEP:Think()
	local chgtime = self:GetModDelay(self.Primary.ChargeTime) 
	if !self.Owner:KeyDown(IN_DUCK) and GAMEMODE:MSCheckForMod("Crouch to Shoot") then
		self:SetCharge(0)
		return
	end
	if self.Owner:KeyPressed(IN_ATTACK) or (self.Owner:KeyDown(IN_ATTACK) and self.Owner:KeyPressed(IN_DUCK) and GAMEMODE:MSCheckForMod("Crouch to Shoot")) then
		self:SetNWFloat("StartChargeTime",CurTime())
	end
	if self.Owner:KeyDown(IN_ATTACK) then
		self:SetCharge((self.Primary.MaxCharge*(math.Clamp(CurTime()-self:GetNWFloat("StartChargeTime",CurTime()),0,chgtime)/chgtime)))
	end
	if self.Owner:KeyReleased(IN_ATTACK) and self:CanPrimaryAttack() then
		self:ReleaseCharge()
		self:SetCharge(0)
	end
end

function SWEP:Initialize()
	self:SetWeaponHoldType( self.HoldType )
	self:SetCharge(0)
end

function SWEP:Holster()
	self:SetCharge(0)
	return true
end

function SWEP:Deploy()
	self:SetCharge(0)
	return true
end

function SWEP:ReleaseCharge()
	self:DetermineCriticalHit()
	if !GAMEMODE:MSCheckForMod("Silent Guns") then self:EmitSound(self:GetModSound()) end
	
	self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )

	self.Owner:MuzzleFlash()
	self.Owner:SetAnimation( PLAYER_ATTACK1 )
	
	if not IsFirstTimePredicted() then return end
	
	con = self:GetModCone()
	dmg = self:GetModDamage(self.Primary.Damage+math.floor(self:GetCharge()))
	missed = false
	if SERVER then
	if GAMEMODE:MSCheckForMod("50% Chance for No Damage") and math.Round(math.random())==1 then 
		missed = true 
		dmg = 0
	end
	end
	
	local bullet = {}
	bullet.Num    = 1
	bullet.Src    = self.Owner:GetShootPos()
	bullet.Dir    = self.Owner:GetAimVector()
	bullet.Spread = Vector( con, con, 0 )
	bullet.Tracer = 1
	bullet.TracerName = self:GetModTracer("supplaser")
	bullet.Force  = 10
	bullet.Damage = dmg
	bullet.Callback = function(attacker, tr, dmginfo)
		self:MetastrikeCallback(attacker, tr, dmginfo) -- Call the default callback first
		if self:GetCharge()>=self.Primary.MaxCharge then
			util.BlastDamage( self, self.Owner, tr.HitPos, 300, 50 )
			
			if CLIENT then
			local effectdata = EffectData()
			 effectdata:SetOrigin( tr.HitPos )
			util.Effect( "Explosion", effectdata, true, true )
			end
		end
	end
	self.Owner:FireBullets( bullet )
	
	

	-- Owner can die after firebullets
	if (not IsValid(self.Owner)) or (not self.Owner:Alive()) or self.Owner:IsNPC() then return end
	
	self:TakePrimaryAmmo( 1 )
	
end

function SWEP:CanPrimaryAttack()
	if self.Owner:GetAmmoCount(self.Primary.Ammo)>0 then
		return true
	else
		self:DryFire(self.SetNextPrimaryFire)
		self:SetCharge(0)
		return false
	end
end

function SWEP:DryFire(setnext)
	self:EmitSound( "Weapon_Pistol.Empty" )

	setnext(self, CurTime() + 0.2)

	self:Reload()
end
